﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;

namespace CutterTool
{
    public partial class frmSetDataSource : Form
    {
        public frmSetDataSource()
        {
            InitializeComponent();
        }

        private void butBrowse_Click(object sender, EventArgs e)
        {
            this.cbTable.Items.Clear();

            openFileDialog1.ShowDialog();
            tbPath.Text = openFileDialog1.FileName;

            if (!tbPath.Text.EndsWith("txt") && !tbPath.Text.EndsWith("csv"))
            {
                this.populateComboBox();
            }
            else
            {
                this.cbTable.Enabled = false;
            }
        }

        private void populateComboBox()
        {
            if (tbPath.Text.Equals(""))
            {
                MessageBox.Show("You must select a database or spreadsheet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //List<string> tables = Worker.getTableList(tbPath.Text);
            //foreach (string s in tables)
            //{
            //    this.cbTable.Items.Add(s);
            //}
        }

        private void butSet_Click(object sender, EventArgs e)
        {
            if (!tbPath.Text.EndsWith("txt") && !tbPath.Text.EndsWith("csv"))
            {
                if (tbPath.Text.Equals("") || cbTable.SelectedItem == null)
                {
                    MessageBox.Show("You must select both a data source and table within that source.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            Properties.Settings.Default.datasourcePath = tbPath.Text;
            Properties.Settings.Default.Save();
            this.Close();
        }
    }
}
